package com.plugin.wukong;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.ganide.utils.WifiConnect;
import com.ganide.wukit.kitapis.KitConfigApi;
import com.ganide.wukit.kits.WukitEventHandler;
import com.ganide.wukit.kits.clibevent.BaseEventMapper;
import com.ganide.wukit.user.KitLanDev;
import com.plugin.wukong.SdkDemoApplication;

import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;

import io.ionic.wukong.R;

public class SmartConfigActivity extends Activity implements WukitEventHandler {
	private EditText etSsid;
	private EditText etPasswd;
	private EditText etSecond;
	private Button btOk;
	private WifiConnect wifiConnect;
	private String ssid;
	private String passwd;
	private int second;
	
	private int timerCount = 0;
	
	private Handler handle = new Handler(new Handler.Callback() {
		
		public boolean handleMessage(Message msg) {
			++timerCount;
			etSecond.setText(String.valueOf(second - timerCount));
			if(timerCount == second) {
				timer.cancel();
			}
			return false;
		}
	});
	private Timer timer = new Timer();
	private TimerTask task = new TimerTask() {
		
		@Override
		public void run() {
			handle.sendEmptyMessage(0);
		}
	};
	
	@Override
	protected void onStart() {
		super.onStart();
		SdkDemoApplication.getKit().registerEvent(BaseEventMapper.SC_BEGIN, BaseEventMapper.SC_END,
				WukitEventHandler.HANDLE_ALL, this);
	};
	
	@Override
	protected void onStop() {
		super.onStop();
		SdkDemoApplication.getKit().unRegisterEvent(this);
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.smart_config);
		
		etSsid = (EditText) findViewById(R.id.et_ssid);
		etPasswd = (EditText) findViewById(R.id.et_passwd);
		etSecond = (EditText) findViewById(R.id.et_second);
		btOk = (Button) findViewById(R.id.bt_ok);
		wifiConnect = new WifiConnect(getApplicationContext());
		
		etSsid.setText(wifiConnect.getSSID());
		etPasswd.setText("4001001234");
		etSecond.setText("240");
	}
	
	public void startConfig(View v) {
		ssid = etSsid.getText().toString();
		passwd = etPasswd.getText().toString();
		String strSec = etSecond.getText().toString();
		try {
			second = Integer.valueOf(strSec);
		}catch (NumberFormatException e){
			finish();
		}

		if (ssid == null || second < 60) {
			Toast.makeText(this, "输入不合法", Toast.LENGTH_LONG).show();
			return;
		}
		btOk.setClickable(false);
		etSecond.setClickable(false);
		SdkDemoApplication.getKit().startSmartConfig(ssid, passwd, KitConfigApi.CONF_MODE_MUT, second);
		timer.schedule(task, 0, 1000);
	}
	
	@Override
	public void onBackPressed() {
		super.onBackPressed();
		SdkDemoApplication.getKit().stopSmartConfig();
	}
	
	public void callback(int skdEvent, int obj_handle, int err_no) {
		switch(skdEvent) {
		case BaseEventMapper.SC_CONFIG_OK:
			ArrayList<KitLanDev> lanDevs = SdkDemoApplication.getKit().getLanDevInfo();
			if (lanDevs != null) {
				boolean found = false;
				for (KitLanDev lanDev : lanDevs) {
					if (lanDev.handle == obj_handle) {
						etSecond.setText(String.valueOf(lanDev.dev_sn) + " 配置成功");
						found = true;
						break;
					}
				}
				if (!found) {
					etSecond.setText("配置成功，但是没找到设备");
				}
			}
			
			etSecond.setClickable(false);
			btOk.setClickable(true);
			timer.cancel();
			break;
		case BaseEventMapper.SC_CONFIG_FAIL:
			etSecond.setText("失败");
			etSecond.setClickable(true);
			btOk.setClickable(true);
			timer.cancel();
			break;
		}
	}
}
